// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ApplicationData.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2010
// </copyright>
// <summary>
//   Defines the ApplicationData type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MtgMC.DataModel
{
    #region Namespace Imports

    using System.IO;

    using Json;

    #endregion


    /// <summary>
    /// Describes and manipulates the application data.
    /// </summary>
    [JsonName("MtgMCData")]
    public class ApplicationData
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ApplicationData"/> class.
        /// </summary>
        public ApplicationData()
        {
            Tournaments = new TournamentCollection();
            Players = new PlayerCollection();
            Locations = new LocationCollection();
            PlayedMatches = new MatchCollection();
        }

        #endregion


        #region Properties

        /// <summary>
        /// Gets the locations.
        /// </summary>
        /// <value>
        /// The locations.
        /// </value>
        [JsonOrder(2)]
        public LocationCollection Locations
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the collection of played matches.
        /// </summary>
        /// <value>
        /// The collection of played matches.
        /// </value>
        [JsonOrder(4)]
        public MatchCollection PlayedMatches
        {
            get;
            private set;
        }


        /// <summary>
        /// Gets or sets the player.
        /// </summary>
        /// <value>
        /// The player.
        /// </value>
        [JsonOrder(0)]
        public Player Player
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the players.
        /// </summary>
        /// <value>
        /// The players.
        /// </value>
        [JsonOrder(1)]
        public PlayerCollection Players
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the tournaments.
        /// </summary>
        /// <value>
        /// The tournaments.
        /// </value>
        [JsonOrder(3)]
        public TournamentCollection Tournaments
        {
            get;
            private set;
        }

        #endregion


        #region Public Methods

        /// <summary>
        /// Loads the specified file name.
        /// </summary>
        /// <param name="fileName">
        /// Name of the file.
        /// </param>
        /// <returns>
        /// Returns an instance of <see cref="MtgMC.DataModel.ApplicationData"/>.
        /// </returns>
        public static ApplicationData Load(string fileName)
        {
            ApplicationData applicationData;

            using (var json = File.OpenText(fileName))
            {
                applicationData = JsonMapper.ToObject<ApplicationData>(json);
            }

            return applicationData;
        }


        /// <summary>
        /// Saves the specified application data.
        /// </summary>
        /// <param name="applicationData">
        /// The application data.
        /// </param>
        /// <param name="fileName">
        /// Name of the file.
        /// </param>
        public static void Save(ApplicationData applicationData, string fileName)
        {
            using (var writer = File.CreateText(fileName))
            {
                string json = JsonMapper.ToJson(applicationData);
                writer.Write(json);
            }
        }

        #endregion
    }
}